package cn.kai.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.kai.base.BaseDO;
import cn.kai.base.BaseService;
import cn.kai.base.TreeNode;
import cn.kai.enums.BooleanEnum;
import cn.kai.mapper.ComfyuiWorkflowMenuChildConfigMapper;
import cn.kai.mapper.ComfyuiWorkflowMenuChildMapper;
import cn.kai.mapper.ComfyuiWorkflowMenuMapper;
import cn.kai.model.ComfyuiWorkflowMenuChildConfigDO;
import cn.kai.model.ComfyuiWorkflowMenuChildDO;
import cn.kai.model.ComfyuiWorkflowMenuDO;
import cn.kai.process.chain.CFRunChain;
import cn.kai.service.ComfyuiWorkflowMenuService;
import cn.kai.util.ConvertUtils;
import cn.kai.util.TreeUtil;
import cn.kai.vo.AiConfigVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 工作流顶层菜单类目 服务实现类
 * </p>
 *
 * @author autoCode
 * @since 2025-02-13 04:44:43
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class ComfyuiWorkflowMenuServiceImpl extends BaseService implements ComfyuiWorkflowMenuService {

    private final ComfyuiWorkflowMenuMapper menuMapper;

    private final ComfyuiWorkflowMenuChildMapper menuChildMapper;

    private final ComfyuiWorkflowMenuChildConfigMapper menuChildConfigMapper;

    private final ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Override
    public AiConfigVO getMenuConfig() {
        AiConfigVO configVO = new AiConfigVO();
        CFRunChain.start(threadPoolTaskExecutor)
                .next(()-> processMenuConfig(configVO))
                .next(()-> processMenuTree(configVO))
                .end();
        //获取
        return configVO;
    }

    /**
     * 处理菜单配置
     */
    private void processMenuConfig(AiConfigVO configVO) {
        //获取所有配置
        List<ComfyuiWorkflowMenuChildConfigDO> list = select(menuChildConfigMapper)
                .where()
                .eq(ComfyuiWorkflowMenuChildConfigDO::getIsUse, BooleanEnum.ONE.getValue())
                .list();
        for (ComfyuiWorkflowMenuChildConfigDO configDO : list) {
            if (!Integer.valueOf(5).equals(configDO.getType())){
                configDO.setCombinationParam(null);
            }
            configDO.setWidgetModifyIndex(null);
        }
        configVO.setConfigMap(
                list.stream().collect(Collectors.groupingBy(ComfyuiWorkflowMenuChildConfigDO::getMenuChildId)
        ));
    }

    /**
     * 处理菜单树
     * @param configVO vo
     */
    private void processMenuTree(AiConfigVO configVO) {
        //获取所有菜单 对菜单进行树化
        List<ComfyuiWorkflowMenuDO> list = menuMapper.select()
                .where()
                .eq(ComfyuiWorkflowMenuDO::getShowFlag, Integer.valueOf(BooleanEnum.ONE.getValue()))
                .list();
        if (CollUtil.isEmpty(list)) {
            return;
        }
        //获取实际菜单
        List<TreeNode> menuTree = select(menuChildMapper)
                .cols(ComfyuiWorkflowMenuChildDO::getMenuId, ComfyuiWorkflowMenuChildDO::getName, ComfyuiWorkflowMenuChildDO::getWeight)
                .where()
                .in(ComfyuiWorkflowMenuChildDO::getMenuId, ConvertUtils.toFieldList(list, BaseDO::getId))
                .list()
                .stream()
                .map(obj -> TreeNode.builder()
                        .id(obj.getId())
                        .parentId(obj.getMenuId())
                        .name(obj.getName())
                        .sort(obj.getWeight())
                        .children(new ArrayList<>())
                        .build())
                .collect(Collectors.toList());
        List<TreeNode> tree = ConvertUtils.listFromTo(list, TreeNode.class);
        tree.addAll(menuTree);
        List<TreeNode> treeNodes = TreeUtil.buildTree(tree);
        configVO.setMenuTree(treeNodes);
    }
}
